//
// Created by root on 24-7-19.
//
#include "../include/opencloseintr.h"
#include "../include/type.h"
#include "../include/print.h"

enum INTR_STATUS get_intr_currentstatus() {
    uint32 eflagstatus = 999;
    uint32 eflags_ifbit = 0x00000200;
    __asm__ __volatile__ ("pushfl;" "popl %0;" :"=g"(eflagstatus)::"memory");
    printk("current eflags status is %d", eflagstatus);
    rollup();
    return eflagstatus & eflags_ifbit ? INTR_ON : INTR_OFF;
}

enum INTR_STATUS open_intr_ret_oldstatus() {
    enum INTR_STATUS oldstatus;
    oldstatus = get_intr_currentstatus();
    if (oldstatus == INTR_ON) { return oldstatus; }
    __asm__ __volatile__ ("sti":::"memory");
    return oldstatus;
}

enum INTR_STATUS close_intr_ret_oldstatus() {
    enum INTR_STATUS oldstatus;
    oldstatus = get_intr_currentstatus();
    if (oldstatus == INTR_OFF) { return oldstatus; }
    __asm__ __volatile__("cli":::"memory");
    return oldstatus;
}

enum INTR_STATUS set_intr_ret_oldstatus(enum INTR_STATUS which_status_set) {
    return which_status_set == INTR_ON ? open_intr_ret_oldstatus() : close_intr_ret_oldstatus();
}
